package com.atguigu.tingshu.order.receiver;


import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.rabbitmq.client.Channel;
import io.micrometer.common.util.StringUtils;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class OrderReceiver {
    @Autowired
    private OrderInfoService orderInfoService;

    @RabbitListener(queues= MqConst.QUEUE_CANCEL_ORDER)
    public void cancelOrder(String orderNo, Message message, Channel channel) {
        log.info("订单取消成功：{}",orderNo);
        //判断
        try {
            if(!StringUtils.isEmpty(orderNo)){
                orderInfoService.cancelOrder(orderNo);
            }
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        //手动签收
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
